package com.cqupt.service.serviceImp;

import com.cqupt.dao.AcountMapper;
import com.cqupt.dao.AdminMapper;
import com.cqupt.dao.ComplaintMapper;
import com.cqupt.dao.UserMapper;
import com.cqupt.paging.Paging;
import com.cqupt.pojo.*;
import com.cqupt.service.AcountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 蒋俊贤
 * @version 1.0
 */
@Service
public class AcountServiceImp implements AcountService {
    @Autowired
    private AcountMapper acountMapper;

    @Autowired
    private ComplaintMapper complaintMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private UserMapper userMapper;

    //获取账户表
    @Override
    public List getAcountList() {

        return acountMapper.selectByExample(null);
    }

    @Override
    //获取账户表分页
    public Paging getAcountList(Integer number){
        AcountExample acountExample = new AcountExample();
        acountExample.createCriteria().andIdBetween(8*(number - 1) + 1,8*number);
        List<Acount> acounts = acountMapper.selectByExample(acountExample);

        AcountExample acountExample1 = new AcountExample();
        acountExample1.createCriteria().andIdIsNotNull();
        List<Acount> acounts1 = acountMapper.selectByExample(acountExample1);
        Paging paging = new Paging();
        paging.setList(acounts);
        paging.setNumber(acounts1.size());
        return paging;

    }

    @Override
    //获取账户信息
    public Acount getAcountInfo(Integer id){
        AcountExample example = new AcountExample();
        example.createCriteria().andIdEqualTo(id);
        List<Acount> acounts = acountMapper.selectByExample(example);
        return acounts.get(0);
    }

    @Override
    //通过用户名获取账户信息
    public Acount getAcount(String name){
        AcountExample example = new AcountExample();
        example.createCriteria().andUsernameEqualTo(name);
        List<Acount> acounts = acountMapper.selectByExample(example);
        return acounts.get(0);
    }

    //获取用户表
    @Override
    public List getUserList() {
        AcountExample example = new AcountExample();
        example.createCriteria().andRootEqualTo(0);
        return acountMapper.selectByExample(example);

    }

    @Override
    //获取用户表分页
    public Paging getUserList(Integer number){
        AcountExample acountExample = new AcountExample();
        acountExample.createCriteria().andRootEqualTo(0);
        List<Acount> acounts = acountMapper.selectByExample(acountExample);
        Paging paging = new Paging();
        if(acounts.size() <= 8){
            paging.setList(acounts);
            paging.setNumber(acounts.size());
            return paging;
        }
        ArrayList<Acount> acountLimit = new ArrayList<>();
        if(acounts.size() <= 8 * number) {
            for (int i = 8 * (number - 1); i < acounts.size(); i++) {
                acountLimit.add(acounts.get(i));
            }
            paging.setList(acountLimit);
            paging.setNumber(acounts.size());
            return paging;

        }

        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            acountLimit.add(acounts.get(i));
        }
        paging.setList(acountLimit);
        paging.setNumber(acounts.size());
        return paging;
    }

    //获取管理员表
    @Override
    public List getAdminList() {
        AcountExample example = new AcountExample();
        example.createCriteria().andRootEqualTo(1);
        return acountMapper.selectByExample(example);
    }

    @Override
    //获取管理员表分页
    public Paging getAdminList(Integer number){
        AcountExample acountExample = new AcountExample();
        acountExample.createCriteria().andRootEqualTo(1);
        List<Acount> acounts = acountMapper.selectByExample(acountExample);
        Paging paging = new Paging();
        if(acounts.size() <= 8){
            paging.setList(acounts);
            paging.setNumber(acounts.size());
            return paging;
        }
        ArrayList<Acount> acountLimit = new ArrayList<>();
        if(acounts.size() <= 8 * number) {
            for (int i = 8 * (number - 1); i < acounts.size(); i++) {
                acountLimit.add(acounts.get(i));
            }
            paging.setList(acountLimit);
            paging.setNumber(acounts.size());
            return paging;

        }

        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            acountLimit.add(acounts.get(i));
        }
        paging.setList(acountLimit);
        paging.setNumber(acounts.size());
        return paging;

    }
    //增加管理员账号
    //1表示成功，2表示用户名已经存在 3表示手机号已经注册
    @Override
    public int addAdmin(String username, String pwd, String phone) {
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andUsernameIsNotNull();
        List<Admin> list = adminMapper.selectByExample(adminExample);
        //用户名是否已经存在
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                return 2;
            }

        }

        //判断电话是否已经注册
        AdminExample adminExample1 = new AdminExample();
        adminExample1.createCriteria().andPhoneIsNotNull();
        List<Admin> list1 = adminMapper.selectByExample(adminExample1);
        for (int i = 0; i < list1.size(); i++) {
            if(list1.get(i).getPhone().equals(phone)){
                return 3;
            }
        }
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setUserpwd(pwd);
        admin.setPhone(phone);
        adminMapper.insert(admin);

        AdminExample adminExample2 = new AdminExample();
        adminExample2.createCriteria().andUsernameEqualTo(username);
        adminExample2.createCriteria().andUserpwdEqualTo(pwd);
        List<Admin> list2 = adminMapper.selectByExample(adminExample2);
        Admin admin2 = list2.get(0);

        Acount acount = new Acount();
        acount.setNumber(admin2.getId());
        acount.setUsername(username);
        acount.setUserpwd(pwd);
        acount.setPhone(phone);
        acount.setRoot(1);


        return acountMapper.insertSelective(acount);
    }

    //增加用户账号
    //1表示成功，2表示用户名已经存在 3表示手机号已经注册
    @Override
    public int addUser(String username, String pwd, String phone) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameIsNotNull();
        List<User> list = userMapper.selectByExample(userExample);
        //用户名是否已经存在
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                return 2;
            }

        }

        //判断电话是否已经注册
        UserExample userExample1 = new UserExample();
        userExample1.createCriteria().andPhoneIsNotNull();
        List<User> list1 = userMapper.selectByExample(userExample1);
        for (int i = 0; i < list1.size(); i++) {
            if(list1.get(i).getPhone().equals(phone)){
                return 3;
            }
        }
        User user = new User();
        user.setUsername(username);
        user.setUserpwd(pwd);
        user.setPhone(phone);
        user.setMoney(0.00);
        user.setScore(0);
        userMapper.insertSelective(user);

        UserExample userExample2 = new UserExample();
        userExample2.createCriteria().andUsernameEqualTo(username);
        userExample2.createCriteria().andUserpwdEqualTo(pwd);
        List<User> list2 = userMapper.selectByExample(userExample2);
        User user1 = list2.get(0);

        Acount acount = new Acount();
        acount.setNumber(user1.getId());
        acount.setUsername(username);
        acount.setUserpwd(pwd);
        acount.setPhone(phone);
        acount.setRoot(0);

        return acountMapper.insertSelective(acount);
    }

    @Override
    //修改账户
    public int updateAcount(Acount acount){
        AcountExample example = new AcountExample();
        example.createCriteria().andIdEqualTo(acount.getId());
        List<Acount> acounts = acountMapper.selectByExample(example);
        Acount acount1 = acounts.get(0);
        if(acount.getUsername() != null){
            acount1.setUsername(acount.getUsername());
        }
        if(acount.getUserpwd() != null){
            acount1.setUserpwd(acount.getUserpwd());
        }

        if (acount.getPhone() != null){
            acount1.setPhone(acount.getPhone());
        }
        acountMapper.updateByPrimaryKeySelective(acount1);

        if(acount.getRoot() == 0){
            User user = new User();
            user.setId(acount.getNumber());
            user.setUsername(acount1.getUsername());
            user.setUserpwd(acount1.getUserpwd());
            user.setPhone(acount1.getPhone());
            return userMapper.updateByPrimaryKeySelective(user);
        }

        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andIdEqualTo(acount1.getNumber());
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);
        admin.setUsername(acount1.getUsername());
        admin.setUserpwd(acount1.getUserpwd());
        admin.setPhone(acount1.getPhone());
        return adminMapper.updateByPrimaryKeySelective(admin);



    }


    @Override
    //删除账户
    public int deleteAcount(Integer id) {
        Acount acount = getAcountInfo(id);
        int root = acount.getRoot();
        if(root == 0){
            userMapper.deleteByPrimaryKey(acount.getNumber());
        }else{
            adminMapper.deleteByPrimaryKey(acount.getNumber());
        }

        return acountMapper.deleteByPrimaryKey(id);
    }

    @Override
    //查看投诉表
    public List complaintList(){
        return complaintMapper.selectByExample(null);

    }

    @Override
    //查看投诉表分页
    public Paging complaintList(Integer number){
        Paging paging = new Paging();
        ComplaintExample complaintExample = new ComplaintExample();
        complaintExample.createCriteria().andIdIsNotNull();
        List<Complaint> complaints = complaintMapper.selectByExample(complaintExample);
        if(complaints.size() <= 8){
            paging.setList(complaints);
            paging.setNumber(complaints.size());
            return  paging;
        }
        List<Complaint> complaintsLimit = new ArrayList<>();
        if(complaints.size() <= 8*number){
            for (int i = 8 * (number - 1); i < complaints.size(); i++) {
                complaintsLimit.add(complaints.get(i));
            }
            paging.setList(complaintsLimit);
            paging.setNumber(complaints.size());
            return paging;
        }
        for (int i = 8 * (number - 1); i < 8 * number; i++) {
            complaintsLimit.add(complaints.get(i));
        }
        paging.setList(complaintsLimit);
        paging.setNumber(complaints.size());
        return paging;

    }
    @Override
    //处理投诉表状态
    public  int complaintState(Integer id){
        ComplaintExample complaintExample = new ComplaintExample();
        Complaint tbcomplaint = new Complaint();
        complaintExample.createCriteria().andIdEqualTo(id);
        List<Complaint> complaints = complaintMapper.selectByExample(complaintExample);
        if(complaints.size() > 0){
            tbcomplaint = complaints.get(0);
        }
        tbcomplaint.setHandle(1);
        return  complaintMapper.updateByPrimaryKeySelective(tbcomplaint);
    }
    @Override
    //投诉时间模糊查询
    public  Paging complaintFuzzySelect(Integer number ,String fuzzy){
        Paging paging = new Paging();
        ComplaintExample complaintExample = new ComplaintExample();
        complaintExample.createCriteria().andTimeLike("%" + fuzzy + "%");
        List<Complaint> complaints = complaintMapper.selectByExample(complaintExample);
        if(complaints.size() <= 8){
            paging.setList(complaints);
            paging.setNumber(complaints.size());
            return  paging;
        }
        List<Complaint> list = new ArrayList<>();
        if(complaints.size() <= 8*number){
            for (int i = 8 * (number - 1); i <complaints.size() ; i++) {
                list.add(complaints.get(i));
            }
            paging.setList(list);
            paging.setNumber(complaints.size());
            return paging;
        }

        for (int i = 8* (number-1); i <8*number ; i++) {
            list.add(complaints.get(i));
        }
        paging.setList(list);
        paging.setNumber(complaints.size());
        return paging;


    }
}
